<?php
/**
 * Translateone Controller for yos_translator Component
 * @package		yos_translator
 * @subpackage	Components
 * @link		http://yopensource.com
 * @author		yopensource
 * @copyright 	yopensource (yopensource@gmail.com)
 * @license		Commercial
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'phpWebHacks.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'find_replace.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'resource_url.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'autocuttext.php');

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
jimport('joomla.filesystem.file');
require_once(JPATH_SITE.DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'archive'.DS.'zip.php');

/**
 * Translator Extension Controller
 *
 * @package		yos_translator
 * @subpackage	Components
 */
class YOS_NewsControllerLink extends YOS_NewsController
{

	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
		$this->registerTask('new', 'edit');
		$this->registerTask('apply', 'save');
	}
	
	function edit(){
		$document 	= &JFactory::getDocument();
		$vType		= $document->getType();

		$vLayout 	= 'edit';
		$mName		= 'linkmanager';
		// Get/Create the view
		$view = &$this->getView( 'linkmanager', $vType);
		// Get/Create the model

		if ($model = &$this->getModel($mName)) {
			// Push the model into the view (as default)

			$view->setModel($model, true);
		}
		// Set the layout
		$view->setLayout($vLayout);
		$view->edit();
	}
	
	/**
	 * Step1: get document type
	 *
	 */
	function get_document_type(){
		$model = &$this->getModel('linkmanager');
		$type=$model->get_document_type();
		echo $type;
		die();
	}
	
	function step2_test(){
		$model = &$this->getModel('linkmanager');
		$links=$model->step2_test();
		echo $links;
		die();
	}
	
	function step2_test_link(){
		$model = &$this->getModel('linkmanager');
		$links=$model->step2_test_link();
		echo $links;
		die();
	}
	
	function step3_test(){		
		$model = &$this->getModel('linkmanager');
		$titles=$model->step3_test();
		echo $titles;
		die();
	}
	
	function step5_test(){
		$model = &$this->getModel('linkmanager');
		$intros=$model->step5_test();
		echo $intros;
		die();
	}
	
	function step5_test_a_link(){
		$model = &$this->getModel('linkmanager');
		$obj_intro=$model->step5_test_a_link();
		echo $obj_intro;
		die();
	}
	
	function step6_test(){
		$model = &$this->getModel('linkmanager');
		$fulls=$model->step6_test();
		echo $fulls;
		die();
	}
	
	function step7_test(){
		$model = &$this->getModel('linkmanager');
		$link_multipage=$model->step7_test();
		echo $link_multipage;
		die();
	}
	
	function step8_test(){
		$model = &$this->getModel('linkmanager');
		$content_multipage=$model->step8_test();
		echo $content_multipage;
		die();
	}	
	function save(){
		global $mainframe;
		
		$model 			=	 &$this->getModel('linkmanager');
		$row			=	$model->save();
		$post			= 	JRequest::get( 'post' );
		switch ( JRequest::getCmd('task') )
		{
			case 'link.apply':
				$msg 	=	JText::_( 'Changes to Contents Link saved' );
				$mainframe->redirect( "index.php?option=".$post['option']."&task=link.edit&cid[]=". $row->id, $msg );
				break;

			case 'link.save':
			default:
				$msg 	=	JText::_( 'Contents Link saved' );
				$mainframe->redirect( "index.php?option=".$post['option']."&task=linkmanager", $msg );
				break;
		}
	}

	function cancel(){
		global $mainframe,$option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		// Set the table directory
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
		$row =& JTable::getInstance('YOS_News_Crawler','Table');
		$row->bind( JRequest::get( 'post' ));
		$row->checkin();
		$mainframe->redirect( "index.php?option=$option&task=linkmanager" );
	}

	function remove(){
		global $mainframe, $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$model = &$this->getModel('linkmanager');
		$model->remove();
		$mess="";
		if ($model->errRemove==null){
			$mess=JText::_('Link(s): Delete is successfully');			
		}
		else {
			$arrErr=$model->errRemove;
			if (count($arrErr)==1) {
					$mess=JText::_("There is");
			}
			else {
					$mess=JText::_("There are");
			}
			$mess.=' '.JText::_('links could not be delete').': ';
			JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
			$row =& JTable::getInstance('YOS_News_Crawler','Table');	
			$ds="";
			foreach ($arrErr as $key=>$jobs_title)
			{
				$row->load($key);
				$mess.=$ds."$key($jobs_title)";
				$ds=", ";
			}
			$mess.=JText::_('. Need to delete any job is using these links');
		}
		$mainframe->redirect( "index.php?option=$option&task=linkmanager", $mess);
	}
/*	*/
	function removelink(){
		global $mainframe, $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$model = &$this->getModel('linkcheck');
		$msg=$model->removelink();
		$mainframe->redirect( "index.php?option=$option&task=linkcheck",$msg );
	}
	
	function removelinkarticle(){
		global $mainframe, $option;
		$model = &$this->getModel('linkcheck');
		$msg=$model->removelinkarticle();
		$mainframe->redirect( "index.php?option=$option&task=linkcheck", $msg);
	}

	function plugin(){
		global $mainframe;
		$link			=	base64_decode(JRequest::getString('link'));
		$pid			=	JRequest::getInt('pid');

		$db	=	&JFactory::getDBO();
		$query		=	"SELECT * FROM #__plugins WHERE id = $pid AND published = 1";
		$db->setQuery($query);
		$row	=	$db->loadObject();

		if (!$row) {
			echo '<font color="red"><strong>Invalid Plugin</strong></div>';
			die();
		}

		$param	=	new JParameter($row->params);

		echo '<input type="hidden" name="pluginreglink" value="'.base64_encode($param->get('reglink','')).'" />';
		echo '<input type="hidden" name="pluginsublink" value="'.$param->get('sublink',0).'" />';
		echo '<input type="hidden" name="pluginregtitle" value="'.base64_encode($param->get('regtitle','')).'" />';
		echo '<input type="hidden" name="pluginsubtitle" value="'.$param->get('subtitle',0).'" />';
		echo '<input type="hidden" name="plugindataway" value="'.$param->get('dataway',0).'" />';
		echo '<input type="hidden" name="pluginregintro" value="'.base64_encode($param->get('regintro','')).'" />';
		echo '<input type="hidden" name="pluginsubintro" value="'.$param->get('subintro',0).'" />';
		echo '<input type="hidden" name="pluginfind_replace_intro" value="'.base64_encode($param->get('find_replace_intro','')).'" />';
		echo '<input type="hidden" name="pluginfind_replace_intro_afterencode" value="'.base64_encode($param->get('find_replace_intro_afterencode','')).'" />';
		echo '<input type="hidden" name="pluginregfull" value="'.base64_encode($param->get('regfull','')).'" />';
		echo '<input type="hidden" name="pluginsubfull" value="'.$param->get('subfull',0).'" />';
		echo '<input type="hidden" name="pluginfind_replace_full" value="'.base64_encode($param->get('find_replace_full','')).'" />';
		echo '<input type="hidden" name="pluginfind_replace_full_afterencode" value="'.base64_encode($param->get('find_replace_full_afterencode','')).'" />';

		die();
	}
	
	function backup(){
		global $mainframe;
		$cid 	= JRequest::getVar('cid', array(0), '', 'array');

		JArrayHelper::toInteger($cid, array(0));
		$row	=	&JTable::getInstance('yos_news_crawler', 'Table');
		$row->load($cid[0]);
		$row->id	=	null;
		$filename	=	base64_decode($row->link);
		$filename	=	preg_replace('/[\.\:]/i', '', $filename);		
		header("Pragma: public");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Expires: 0");
		
		header("Content-Transfer-Encoding: binary");
		header('Content-Disposition: attachment;'
		. ' filename="'.$filename.'.xml";'
		. ' modification-date="' . date('Y-m-d H:i:s') . '";'
		); //RFC2183
		header("Content-Type: application/x-download");			// MIME type
		echo $row->toXML();
		die();
	}	
	function archive() {
		$cid 			= JRequest::getVar('cid', array(0), '', 'array');
		JArrayHelper::toInteger($cid, array(0));		
		
		$row			= &JTable::getInstance('yos_news_crawler', 'Table');		
		$path_archive	= JPATH_COMPONENT_ADMINISTRATOR.DS.'install'.DS;		
		$fileinfos		= array();
		for ($i = 0; $i<count($cid); $i++) {			
			$row->load($cid[$i]);			
			$row->id	= null;
			if ($row->pid) {
				$row->pid=$this->id2element($row->pid);
			}
			else {
				$row->pid="(YOS)no plugin";
			}
			
			$host		= parse_url(($row->link));
			if (!isset($host['host']))
				continue;				
			$filename	= ($row->link);
			$filename	= preg_replace('/[\.\:]/i', '', $filename);
			$filename	= preg_replace('/[\/\?]/i', '_', $filename);
//			$filename	= preg_replace('/www./i', '', $host['host']);
			$xml_file 	= $path_archive.$filename.'.xml';
			JFile::write($xml_file, $row->toXML());

			$fileinfos[$i]['data']	= JFile::read($xml_file);
			$fileinfos[$i]['name']	= $filename.'.xml';
			JFile::delete($xml_file);
		}		
		$filename 	= 'YOS_news_crawler_backup_links';
    	
		$filezip 	= $path_archive.$filename.'.zip';
		$zip 		= new JArchiveZip();
		$zip->create($filezip,$fileinfos);

		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: public");
    	header("Content-Description: File Transfer");		
		header("Content-Type: application/zip");
		header("Content-Disposition: attachment; filename=".basename($filezip).";");
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".filesize($filezip));
		@readfile($filezip);
    	exit;
		die();
	}
		// FUNCTION CONVERT FOR PLUGIN
	function id2element($id)
	{
		$db=JFactory::getDBO();
		$query='SELECT element 
				FROM #__plugins 
				WHERE id='.$id;
		$db->setQuery($query);
		$elementName=$db->loadResult();		
		return $elementName;
	}
}

